﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace FieldUpdater.Classes
{
    public enum PageMode
    {
        New,
        Edit
    }
    public enum ConnectionTypes
    {
        SharePointLookup,
        Oracle,
        DateDay
    }
    public static class Global
    {
        /// <summary>
        /// Method to convert a custom Object to XML string
        /// </summary>
        /// <param name="pObject">Object that is to be serialized to XML</param>
        /// <returns>XML string</returns>
        public static string SerializeObject<FType>(FType pObject)
        {
            try
            {
                String XmlizedString = null;
                MemoryStream memoryStream = new MemoryStream();
                XmlSerializer xs = new XmlSerializer(typeof(FType));
                XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

                xs.Serialize(xmlTextWriter, pObject);
                memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
                XmlizedString = Global.UTF8ByteArrayToString(memoryStream.ToArray());
                return XmlizedString;
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e);
                return null;
            }
        }

        /// <summary>
        /// Method to reconstruct an Object from XML string
        /// </summary>
        /// <param name="pXmlizedString"></param>
        /// <returns></returns>
        public static FType DeserializeObject<FType>(string pXmlizedString)
        {
            XmlSerializer xs = new XmlSerializer(typeof(FType));
            MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

            return (FType)xs.Deserialize(memoryStream);
        } 

        /// <summary>
        /// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
        /// </summary>
        /// <param name="characters">Unicode Byte Array to be converted to String</param>
        /// <returns>String converted from Unicode Byte Array</returns>
        public static String UTF8ByteArrayToString(Byte[] characters)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            String constructedString = encoding.GetString(characters);
            return (constructedString);
        }

        /// <summary>
        /// Converts the String to UTF8 Byte array and is used in De serialization
        /// </summary>
        /// <param name="pXmlString"></param>
        /// <returns></returns>
        public static Byte[] StringToUTF8ByteArray(String pXmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(pXmlString);
            return byteArray;
        }

        /// <summary>
        /// Get a list of fields that have a third party field association
        /// </summary>
        /// <returns></returns>
        public static string GetFieldAssociatedConnecions()
        {
            return string.Format("{0}:Normal,{1}:Oracle", ConnectionTypes.SharePointLookup, ConnectionTypes.Oracle);
        }

        public static string Combine(this IEnumerable<string> str, char delimiter)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var st in str)
            {
                sb.Append(st);
                sb.Append(delimiter);
            }
            return sb.ToString().TrimEnd(delimiter);
        }
    }
}